package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: 李旭
 * @create: 2021-07-09 14:32
 **/
@RestController
@RequestMapping("/api/item")
//@RefreshScope
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;
    //获取商品详情页面上所有需要的数据
    @GetMapping("/getItem/{skuId}")
    public  Map getItem(@PathVariable Long skuId){
        Map map = new HashMap<>();
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //1：查询SkuInfo 根据skuId
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);
        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //2：查询 分类视图  条件：三级分类的ID
            BaseCategoryView baseCagegoryView = productFeignClient.
                    getBaseCagegoryView(skuInfo.getCategory3Id());
            map.put("categoryView", baseCagegoryView);
        }, threadPoolExecutor);
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //3:单独查询价格  注意事项：不可以优化 不可以放入缓存  BigDecimal: 钱方面的
            BigDecimal price = productFeignClient.getPrice(skuId);
            map.put("price", price);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4：根据spuId,skuId 查询销售属性及属性值  集合  销售属性及值的红边框  SQL
            List<SpuSaleAttr> spuSaleAttrAndValue = productFeignClient.
                    getSpuSaleAttrAndValue(skuInfo.getSpuId(), skuInfo.getId());
            map.put("spuSaleAttrList", spuSaleAttrAndValue);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //5:组合数据   返回值 Map  没有可使用的对象的时候 使用Map  无敌版对象  万能对象
            Map saleAttrValuesBySpu = productFeignClient.getSaleAttrValuesBySpu(skuInfo.getSpuId());
            map.put("valuesSkuJson", JSONObject.toJSONString(saleAttrValuesBySpu));
        }, threadPoolExecutor);
        //6:更新热度
        CompletableFuture.runAsync(() -> {
            listFeignClient.hotScore(skuId);
        },threadPoolExecutor);
        //多任务组合
        CompletableFuture.allOf(valuesSkuJsonCompletableFuture,
                spuSaleAttrListCompletableFuture,priceCompletableFuture,
                categoryViewCompletableFuture).join();
        return map;
    }
}
